package com.hb.config.web;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * 请求配置类
 */
@Configuration
public class HbplatWebConfig {
    /**
     * 当前服务器IP或者域名
     */
    @Value("${server.address}")
    private String address;
    /**
     * 当前服务使用端口
     */
    @Value("${server.port}")
    private int port;
    /**
     * 当前服务的上下文路径
     */
    @Value("${server.servlet.context-path}")
    private String context;
    /**
     * Swagger Title
     */
    @Value("${hb.swagger.title:Swagger Title}")
    private String title;
    /**
     * Swagger description
     */
    @Value("${hb.swagger.description:Swagger description}")
    private String description;
    /**
     * Swagger contact
     */
    @Value("${hb.swagger.contact:Mike}")
    private String contact;
    /**
     * Swagger email
     */
    @Value("${hb.swagger.email:1@qq.com}")
    private String email;
    /**
     * Swagger version
     */
    @Value("${hb.swagger.version:v1.01}")
    private String version;

    /**
     * 实例化配置项
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(title, description,
                        address, port, context,
                        contact, email, version))
                .select()
                // swagger要显示的包
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any()).build();
    }

    /**
     * 声明其他相关属性信息
     *
     * @return
     */
    public ApiInfo apiInfo(String title, String description,
                           String address, int port, String context,
                           String contact, String email, String version) {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(address + ":" + port + context)
                .contact(new Contact(contact,
                        address + ":" + port + context + "/swagger-ui.html", email))
                .version("版本号: " + version).build();
    }
}
